#!/bin/bash

errs=
progname="$(basename $0)"
file=/usr/include/mysql/mysql.h
[ -f ${file} ] || {
	echo "${progname}: MySQL/Development not installed
	missing: >>${file}<<" >&2
	errs=yes
}

file=/usr/lib/mysql/libmysqlclient_r.so
[ -f ${file} ] || file=/usr/lib/mysql/libmysqlclient.so
[ -f ${file} ] || {
	echo "${progname}: MySQL/Shared not installed
	missing: >>${file}<<" >&2
	errs=yes
}

[ -z "${errs}" ] || exit 2
usage="usage: ${progname} [-v gawk_version] [-I] [-A] [base_directory]"

version="3.1.7"
install=
awk=yes

while getopts ":v:IAh" arg
do
	case "${arg}" in
	v)
		version="${OPTARG}"
		;;
	I)
		install=yes
		;;
	A)
		awk=
		;;
	h)
		echo "${usage}" >&2
		exit 0
		;;
	\?)
		errs=yes
		;;
	esac
done

shift $((${OPTIND} - 1))
case $# in
0)
	base=~/SPAWK
	;;
1)
	base="${1}"
	;;
*)
	errs=yes
	;;
esac

[ -z "${errs}" ] || {
	echo "${usage}" >&2
	exit 1
}

archive="spawk.tar"
[ -f "${archive}" ] || {
	echo "${progname}: ${archive}: file not found" >&2
	exit 2
}

mkdir "${base}" 2>/dev/null || {
	echo "${progname}: ${base}: cannot create base directory \
for this installation" >&2
	exit 2
}

echo -n "
Installing SPAWK"
[ -n "${awk}" ] && echo -n " for GAWK version ${version}"
echo ".
Error messages for this installation will be diverted to file:
	>> ${base}/install.error <<
"

echo "Creating \`${base}' installation directory..."
mkdir "${base}"/spawk || exit 2
exec 2>"${base}"/install.error
mv "${archive}" "${base}"/spawk || exit 2
cd "${base}" || exit 2

errors() {
	echo "

Errors encountered. Please check \`${base}/install.error' file."
}
trap "errors" 0

# If awk sources are already installed, then this script may
# run with the -A option. If not given the -A option, then
# `awk' variable must be set to "yes" and now is the time
# to install awk sources from the web.
if [ -n "${awk}" ]; then
	echo -n "Downloading GAWK version ${version} from ftp.gnu.org..."
	wget --quiet http://ftp.gnu.org/gnu/\
gawk/gawk-"${version}".tar.gz || exit 2
	echo

	echo -n "Configuring GAWK version ${version} for this machine..."
	set -o pipefail
	gunzip -qc gawk-"${version}".tar.gz | tar xp || exit 2
	rm -f xpf gawk-"${version}".tar.gz
	mv gawk-"${version}" gawk || exit 2
	cd gawk || exit 2
	sh ./configure >/dev/null || exit 2
	[ -n "${install}" ] && {
		echo -n "
Building GAWK version ${version} for this machine..."
		make >/dev/null || exit 2
		echo -n "
Checking GAWK version ${version}..."
		make check >/dev/null || exit 2
		echo -n "
Installing GAWK version ${version}..."
		make install-strip exec_prefix="/" >/dev/null || exit 2
	}
	echo
fi

echo -n "Installing SPAWK files..."
cd "${base}"/spawk || exit 2
tar -xzf spawk.tar || exit 2
rm -f spawk.tar
echo

# If given the -A option to this script, then awk sources are
# already installed. We are not proceed in the SPAWK make now
# but we may do that later, ater editing the SPAWK Makefile,
# seting the `AWKDIR' make variable to the correct directory.
if [ -n "${awk}" ]; then
	echo -n "Creating SPAWK extension libraries..."
	make >/dev/null || exit 2
	echo
else
	echo "
Change directory to \`"${base}"/spawk' and edit
the Makefile. After setting the \`AWKDIR' to the awk sources
full (or relative) pathname, run make to build the SPAWK
extension libraries.
"
fi

echo -n "SPAWK installation "
[ -n "${awk}" ] && echo -n "for GAWK version ${version} "
echo -n "finished"
if [ -s "${base}"/install.error ]; then
	echo " with errors.
Please check \`${base}/install.error' for errors."
else
	echo " successfully!"
	rm -f "${base}"/install.error
fi

trap "" 0
exit 0
